import 'package:common/common.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get_navigation/src/router_report.dart';

import 'router_history_manager.dart';

final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>();

/// 手动让getx感知原生路由 (固定代码无需修改)
class GetXRouterObserver extends NavigatorObserver {
  @override
  void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
    RouterReportManager.reportCurrentRoute(route);
    RouterHistoryManager().putRouterByName(route.settings.name);
    logs('添加之后-当前的My路由表：${RouterHistoryManager().routeNames.toString()}');
  }

  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) async {
    RouterReportManager.reportRouteDispose(route);
    RouterHistoryManager().removeRouterByName(route.settings.name);
    logs('Pop之后-当前的My路由表：${RouterHistoryManager().routeNames.toString()}');
  }

  @override
  void didRemove(Route route, Route? previousRoute) {
    RouterHistoryManager().removeRouterByName(route.settings.name);
    logs('Remove之后-当前的My路由表：${RouterHistoryManager().routeNames.toString()}');
  }

  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    RouterHistoryManager().putRouterByName(newRoute?.settings.name);
    RouterHistoryManager().removeRouterByName(oldRoute?.settings.name);
    logs('Replace之后-当前的My路由表：${RouterHistoryManager().routeNames.toString()}');
  }
}
